package com.mail.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.api.PmsBrandApi;
import com.mail.model.db.PmsBrand;
import com.mail.model.vo.PageResult;
import com.mail.server.exception.BusinessException;
import com.mail.server.exception.ErrorResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsBrandService {
    @DubboReference
    private PmsBrandApi brandApi;

    //获取全部品牌列表
    public List<PmsBrand> listAll() {
        return brandApi.findAllBrand();
    }

    //根据品牌名称分页获取品牌列表
    public PageResult list(Integer pageNum, Integer pageSize, String keyword) {
        Page page = brandApi.pageByKeyword(pageNum, pageSize, keyword);
        return new PageResult(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
    }

    //根据编号查询品牌信息
    public PmsBrand query(Long id) {
        return brandApi.findById(id);
    }

    //更新品牌
    public Integer updateBrand(Long id, PmsBrand brand) {
        brand.setId(id);
        return brandApi.updateBrand(brand);
    }

    //批量更新显示状态
    public void updateShowStatus(List<Long> ids, Integer showStatus) {
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                PmsBrand brand = new PmsBrand();
                brand.setId(id);
                brand.setShowStatus(showStatus);
                Integer count = brandApi.updateBrand(brand);
                if (count == null) {
                    throw new BusinessException(ErrorResult.error());
                }
            }
        }
    }

    //批量更新厂家制造商状态
    public void updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        if (CollUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                PmsBrand brand = new PmsBrand();
                brand.setId(id);
                brand.setFactoryStatus(factoryStatus);
                Integer count = brandApi.updateBrand(brand);
                if (count == null) {
                    throw new BusinessException(ErrorResult.error());
                }
            }
        }
    }

    //批量删除品牌
    public Integer deleteBatch(List<Long> ids) {
        return brandApi.deleteByIds(ids);
    }

    //删除指定品牌
    public Integer deleteBrand(Long id) {
        List<Long> list = new ArrayList<>();
        list.add(id);
        return brandApi.deleteByIds(list);
    }

    //添加品牌
    public Integer create(PmsBrand brand) {
        return brandApi.save(brand);
    }
}
